<!-- 添加标签 -->
<template>
  <el-dialog :loading="loading" class="gl_dialog_TitleLine" width="660px" title="添加身份标签" :visible.sync="dialogVisible" :close-on-click-modal="false">
    <div v-if="registerTag.length > 0">
      <el-checkbox-group v-if="dialogVisible" v-model="tagCode" size="mini">
        <el-checkbox v-for="tag in registerTag" :key="tag.code" :label="tag.code" border="">{{ tag.value }}</el-checkbox>
      </el-checkbox-group>
    </div>
    <div v-else>
      暂无可添加的身份标签
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" size="mini" @click="addTag">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import {
  api_GET_EventsManagementRegisterTag,
  api_POST_EventsManagementRegisterTagTags
} from '@/extend/api/gameswaggerApi/app.js';
import {
  Api_GET_EventsManagementRegisterTagQO,
  SiSTKWEventsManagementCreateRegisterTagsInput
} from '@/lib/form/game/swaggerForm';
import { mapState, mapActions } from 'vuex';
export default {
  name: 'AddTag',
  data() {
    return {
      loading: false,
      dialogVisible: false,
      Api_GET_EventsManagementRegisterTagQO: new Api_GET_EventsManagementRegisterTagQO(),
      SiSTKWEventsManagementCreateRegisterTagsInput: new SiSTKWEventsManagementCreateRegisterTagsInput(),
      registerTagType: [], // 标签字典
      registerTagTypeCode: [], // 全部的标签code
      registerTag: [], // 可添加的标签列表
      tagCode: [] // 添加的标签code
    };
  },
  computed: {
    ...mapState({
      dict: state => state.dict.dict
    })
  },
  created() {
    this.getTag();
    this.getDict().then(res => {
      this.registerTagType = res.registerTagType;
    });
  },
  methods: {
    ...mapActions({ getDict: 'dict/getDict' }),
    open() {
      this.dialogVisible = true;
    },
    // 获取标签
    getTag() {
      this.loading = true;
      var that = this;
      this.Api_GET_EventsManagementRegisterTagQO.RegisterId = this.$route.query.id;
      api_GET_EventsManagementRegisterTag(this.Api_GET_EventsManagementRegisterTagQO).then(res => {
        that.registerTagTypeCode = res.items.map(function(item) {
          return item.registerTagTypeCode;
        });
        that.registerTag = that.registerTagType.filter(item => !this.registerTagTypeCode.includes(item.code));
        that.loading = false;
      }).catch(() => {
        that.loading = false;
      });
    },
    // 添加标签
    addTag() {
      this.loading = true;
      const tags = [];
      if (this.tagCode.length > 0) {
        for (let i = 0; i < this.tagCode.length; i++) {
          tags.push({
            registerId: this.$route.query.id,
            registerTagTypeCode: this.tagCode[i]
          });
        }
        this.SiSTKWEventsManagementCreateRegisterTagsInput.tags = tags;
        api_POST_EventsManagementRegisterTagTags(this.SiSTKWEventsManagementCreateRegisterTagsInput).then(res => {
          this.$message({
            type: 'success',
            message: '新增标签成功'
          });
          this.getTag();
          this.loading = false;
          this.$parent.getInfo();
        }).catch(() => {
          this.loading = false;
        });
      }
      this.dialogVisible = false;
    }
  }
};
</script>
